//
//  DECXSearchStoreOrderController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/10/9.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXSearchStoreOrderController.h"
#import "DECXOrderTableViewCell.h"
#import "DECXManager.h"
#import "DECXOrderDetailsViewController.h"

@interface DECXSearchStoreOrderController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *searchTable;
@property (strong, nonatomic) NSArray *dataAry;
@property (strong, nonatomic) DECXManager *manager;
@property (weak, nonatomic) IBOutlet UITextField *searchTxf;
@property (weak, nonatomic) IBOutlet UIButton *searchTypeBtn;
@property (strong, nonatomic) UIView *resultView;
@property (assign, nonatomic) NSInteger searchType;
@end

@implementation DECXSearchStoreOrderController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.manager = [DECXManager new];
    [self.searchTxf becomeFirstResponder];
    [self.view addSubview:self.resultView];
    self.resultView.hidden = NO;
    self.searchTable.hidden = YES;
    self.searchType = 1;
}
- (void)fetchOrderListByWord:(NSString *)word
{
    if (self.searchTxf.text.length == 0) {
        return;
    }
    MyUser;
    weak_Self(self);
    [[self.manager searchOrderByGetListID:word depot:user.depot_id type:self.searchType]subscribeNext:^(id x) {
        weakSelf.dataAry = x;
        if (weakSelf.dataAry.count == 0) {
            weakSelf.resultView.hidden = NO;
            weakSelf.searchTable.hidden = YES;
        }else{
            weakSelf.resultView.hidden = YES;
            weakSelf.searchTable.hidden = NO;
        }
        [weakSelf.searchTable reloadData];
    } error:^(NSError *error) {
//        NSString *msg = error.userInfo[@"msg"];
//        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
- (UIView *)resultView
{
    if (!_resultView) {
        _resultView = [[UIView alloc]init];
        [_resultView setFrame:CGRectMake(0, NAVBAR_HEIGHT+STATUSBAR_HEIGHT, ScreenWidth, ScreenHeight-(NAVBAR_HEIGHT+STATUSBAR_HEIGHT))];
        UILabel *lbl = [UILabel new];
        lbl.text = @"无结果";
        lbl.textAlignment = NSTextAlignmentCenter;
        [lbl setFrame:CGRectMake(0, 120, ScreenWidth, 30)];
        [_resultView addSubview:lbl];
    }
    return _resultView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"OrderCell";
    
    DECXOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        cell = [[DECXOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    DECXStoreOrder *order = self.dataAry[indexPath.row];
    [cell setOrderInfo:order];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DECXStoreOrder *order = self.dataAry[indexPath.row];
    DECXOrderDetailsViewController *vc = StoryBoardViewController(@"Main", @"DECXOrderDetailsViewController");
    vc.list_id = order.list_id;
    vc.pushKey = @"Search";
    [self.navigationController pushViewController:vc animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self fetchOrderListByWord:textField.text];
    return YES;
}
- (IBAction)cancelClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)searchTypeClick:(id)sender
{
    if (self.searchType == 1) {
        self.searchType = 2;
        [self.searchTypeBtn setTitle:@"货权人" forState:UIControlStateNormal];
        self.searchTxf.placeholder = @"请输入货权人姓名";
    }else if (self.searchType == 2){
        self.searchType = 1;
        [self.searchTypeBtn setTitle:@"提单号" forState:UIControlStateNormal];
        self.searchTxf.placeholder = @"请输入提单号";
    }
}
#pragma mark --- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length!=0) {
        [self fetchOrderListByWord:toBeString];
    }
    return YES;
}

@end
